package com.nyist.diseasepreventionserver.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.nyist.diseasepreventionserver.VO.ShowapiVO;
import lombok.*;

import java.io.Serializable;

/**
 * @Auther: 杜習田
 * @Date: 2019/12/12 10:54
 * @Description:返回数据格式封装类
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
//保证序列化json的时候,如果是null的对象,key也会消失
public class ServerResponse<T> implements Serializable {

    private int status;
    private String msg;
    private T data;


    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return new ServerResponse<T>(ResponseCode.success.getCode(),msg,null);
    }

    public static <T> ServerResponse<T> createBySuccess(T data){
        return new ServerResponse<T>(ResponseCode.success.getCode(),ResponseCode.success.getDesc(),data);
    }


    public static <T> ServerResponse<T> createBySuccess(String msg, T data){
        return new ServerResponse<T>(ResponseCode.success.getCode(),msg,data);
    }



    public static <T> ServerResponse<T> createByError(){
        return new ServerResponse<T>(ResponseCode.error.getCode(),ResponseCode.error.getDesc(),null);
    }


    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
        return new ServerResponse<T>(ResponseCode.error.getCode(),errorMessage,null);
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage){
        return new ServerResponse<T>(errorCode,errorMessage,null);
    }


}
